package org.ysh.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.ysh.core.annotation.ResourceEx;
import org.ysh.core.cache.Constants;
import org.ysh.core.web.TableDataInfo;
import org.ysh.domain.Fonds;
import org.ysh.service.FondsService;
import org.ysh.vo.FondsTreeNodeVo;
import org.ysh.vo.R;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/system/fonds")
public class FondsController extends BaseController{

    @Resource
    private FondsService fondsService;

    @RequiresPermissions("system:fonds:list")
    @ResourceEx(name = "全宗管理", description = "全宗管理",code = "system.fonds", parentCode = "system", type = 0, icon = "fa fa-book", sort = 0)
    @RequestMapping("/list")
    public String list() {
        return "/system/fonds/list";
    }


    @RequiresPermissions("system:fonds:list")
    @ResourceEx(name = "列表", description = "全宗管理-列表",code = "system.fonds.list", parentCode = "system", type = 1, icon = "fa fa-book", sort = 0 , permission = "system:fonds:list")
    @GetMapping("/fonds_list")
    @ResponseBody
    public TableDataInfo fondsList() {
        return getDataTable(fondsService.getFondsVoList());
    }

    @RequiresPermissions("system:fonds:add")
    @ResourceEx(name = "新增", description = "全宗管理-新增全宗", code = "system.fonds.add", parentCode = "system.fonds", type = 1, icon = "fa fa-plus-square", sort = 1, permission = "system:fonds:add")
    @GetMapping("/add")
    public String add() {
        return "/system/fonds/add";
    }

    @RequiresPermissions("system:fonds:add")
    @ResponseBody
    @PostMapping("/add")
    public R<?> add(Fonds fonds) {
        List<Fonds> fondsList = fondsService.findByPage(new Fonds(fonds.getFondsCode()));
        if(!fondsList.isEmpty()) {
            return R.fail("全宗号已存在!");
        }

        fondsService.insert(fonds);
        return R.ok();
    }

    /**
     * 获取全宗树
     * @return
     */
    @RequiresPermissions("system:fonds:list")
    @ResponseBody
    @GetMapping("/fonds_tree")
    public List<FondsTreeNodeVo> getFondsTree() {
        return fondsService.getFondsTree();
    }

    @RequiresPermissions("system:fonds:edit")
    @ResourceEx(name = "编辑", description = "全宗管理-编辑全宗", code = "system.fonds.edit", parentCode = "system.fonds", type = 1, icon = "fa fa-edit", sort = 2, permission = "system:fonds:edit")
    @GetMapping("/edit")
    public ModelAndView edit(String fondsCode) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("fonds", fondsService.findByPage(new Fonds(fondsCode)).get(0));
        mv.setViewName("/system/fonds/edit");
        return mv;
    }

    @RequiresPermissions("system:fonds:edit")
    @ResponseBody
    @PostMapping("/edit")
    public R<?> edit(Fonds fonds) {
        if(fonds.getId() == null){
            return R.fail("全宗ID不能为空!");
        }
        fondsService.update(fonds);
        return R.ok();
    }

    @RequiresPermissions("system:fonds:delete")
    @ResourceEx(name = "删除", description = "全宗管理-删除全宗", code = "system.fonds.delete", parentCode = "system.fonds", type = 1, icon = "fa fa-minus-square", sort = 3, permission = "system:fonds:delete")
    @ResponseBody
    @PostMapping("/delete")
    public R<?> delete(String fondsCode) {
        fondsService.deleteByFondsCode(fondsCode);
        return R.ok();
    }


    /**
     * 切换全宗
     * @param fondsCode
     * @return
     */
    @RequestMapping("/switch")
    @ResponseBody
    public R<?> switchFonds(String fondsCode) {
        SecurityUtils.getSubject().getSession().setAttribute(Constants.CURRENT_FONDS,fondsCode);
        return R.ok();
    }

}
